package com.ruoyi.zq.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.zq.domin.dto.ZqNoticeDTO;
import com.ruoyi.zq.domin.vo.NoticeDetailsVO;
import com.ruoyi.zq.service.ZqNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 修改通知后的通知接口
 *
 * @Author: LoneyFeather
 * @Date: Created in 15:17 2021/12/30
 */
@Api(tags = "通知接口修改后")
@RestController
@RequestMapping("/notice")
public class ZqNoticeDetialsController extends BaseController {

    @Autowired
    private ZqNoticeService noticeService;

    @ApiOperation("获取通知公告列表")
    @PreAuthorize("@ss.hasPermi('zq:notice:list')")
    @GetMapping("/list")
    public TableDataInfo list(NoticeDetailsVO noticeVO) {
        startPage();
        List<NoticeDetailsVO> list = noticeService.selectNoticeList(noticeVO);
        return getDataTable(list);
    }


    @ApiOperation("根据通知公告编号获取详细信息")
    @PreAuthorize("@ss.hasPermi('zq:notice:query')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId) {
        return AjaxResult.success(noticeService.selectNoticeById(noticeId));
    }


    @ApiOperation("新增通知公告")
    @PreAuthorize("@ss.hasPermi('zq:notice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ZqNoticeDTO noticeDTO) {
        noticeDTO.setCreateBy(getUsername());
        noticeDTO.setCreateTime(new Date());
        return toAjax(noticeService.addNoticeAndDetails(noticeDTO));
    }


    @ApiOperation("修改通知公告")
    @PreAuthorize("@ss.hasPermi('zq:notice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody ZqNoticeDTO noticeDTO){
        if(noticeDTO.getNoticeId() ==null){
            return AjaxResult.error("参数缺少重要参数，noticeID");
        }
        noticeDTO.setUpdateBy(getUsername());
        noticeDTO.setUpdateTime(new Date());
        return toAjax(noticeService.updateNotice(noticeDTO));
    }

    @ApiOperation("删除通知公告")
    @PreAuthorize("@ss.hasPermi('zq:notice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeIds}")
    public AjaxResult remove(@PathVariable Long[] noticeIds) {
        return toAjax(noticeService.deleteNoticeByIds(noticeIds));
    }




}
